package cn.edu.cdp.dumpling.controller;

import cn.edu.cdp.dumpling.entity.Account;
import cn.edu.cdp.dumpling.entity.Consumer;
import cn.edu.cdp.dumpling.service.ConsumerService;
import cn.edu.cdp.dumpling.util.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 客户管理
@RestController
//@CrossOrigin
@RequestMapping("/consumers")
public class ConsumerController {
    private final ConsumerService consumerService;

    public ConsumerController(ConsumerService consumerService) {
        this.consumerService = consumerService;
    }

    // 登录
    @PostMapping("/login")
    public R login(@RequestBody Account account) {
//        System.out.println(account);
        Consumer c = consumerService.login(account);
        if (c == null) {
            return R.error("登录失败");
        } else {
            return R.success("登录成功", c);
        }
    }

    // 查看
    @GetMapping
    public R listAll() {
        List<Consumer> list = consumerService.list();
        if (list == null || list.isEmpty())
            return R.error("查询失败");
        else
            return R.success("查询成功", list);
    }

    // 增加
    @PostMapping
    public R save(@RequestBody Consumer consumer) {
        if (consumerService.save(consumer))
            return R.success("添加成功", null);
        else
            return R.error("添加失败");
    }

    // 删除
    @DeleteMapping("{id}")
    public R removeById(@PathVariable Integer id) {
        if (consumerService.removeById(id))
            return R.success("删除成功", null);
        else
            return R.error("删除失败");
    }

    // 修改客户资料
    @PutMapping
    public R updateById(@RequestBody Consumer consumer) {
        if (consumerService.updateById(consumer))
            return R.success("修改成功", null);
        else
            return R.error("修改失败");
    }
}
